package com.mall.order.conf;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000116671433";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCInFTPUQc9XK6+QHjM/Dk/0g40arlctLwUjJl9LvI5heFBfYHWHgX2ayY1rEBhSbnCgW1SIsSC8CO3Po9SRb4f9rR4FZiNlUiRHVjfrmT6OBrc58EBeQTgX9eehKstVgRu/3tqAprp11FnXosmC7yGkz+6s1E2M+BLBbktM6WRnuWhW0B2+9mUzRFFxm8I64tOgYnN7aUrK18OXrEhSrvysCtRVewe7+tpM74Ah7n0Xp51K2zE7xq+ZEDqQx4ZPvcjOUe36b28L2gBNoXb4f2S0vEn8KaITq+2fQFsDeSbnDhmdTg3aCDm/sfQKFcDSOyyluCQgcHpf0XAdEap7N3xAgMBAAECggEAChlsl6sI2vt4G66Of7EB1nsD+9IMBBOEeAxw6HVJCQGhxoKtz5OGo1IGkB883zRhHEUPPOw/FOMF8O0aEV4AkUJe0yXdklH//GJsCJVO98SgD8a+WQqGVsQfH6P1N7PNPht7Fzo8E5+McBToOpbRiC1on7fUACrToRg6OWJBEkPOIFKJhk/7btcYO1dnBF3QTaVjUIKNgU9iCb/xGthyGPOfLxps4uQ8FsrQO688Xlc1CvkkKkfMDcisk2QiY5X/TjSo64TDspOIIKe3dUAlmGzM2KrdQZFGvnl1AIYq1QVYTiIkWRTMihOnfo2n72y1Tl0fau3jQPxgZO41MzKEQQKBgQDTZDMFq3tdsnf9GWT8WgD4xWmsHkbD2VkuAWioBmc3f8ePLnz/RDkv3vamfcMp9jdEkvNOFW+hjF0YfZ8kZ45opNVBEps1xBRDHjz1syHX8Rl3Zh4XXcD4pvixCmvPYtwQL6VxBEFAyU69xfr3Hn6J0UmLily8cpTl0Zd9Qcd06QKBgQClcFJ0jgZIe23NnJMz0Xs1F/vCHDAnBSi4yM0YBD63/2tt+GLivtyeUbUIX3KAliNeNPQptNYpske0+E5+wUGnytlfWexz0eRd45B2YEBXDtBkmONrKSbStUJLT1ediKHdGydbqajgfg3L5C3FfzO04X2jOLHFpJZKGEFvqqGbyQKBgQDHTVghh1znqSUhtriUh9MUuSN5SI5Vhm1jR6bBjE/yboHax+zHyy3TqXaI4FeKD35qI9ZLD0DRYgI/3eiWLSCCHq6FDKLTXUvxP+gfisVk0Aa/oMLMKvOOO/aLDJU60S1EAXgNBwLyrxmTKL8GEHUjQPQDIO9wKOOjjikyyjrgSQKBgGZdfLef1WVt8gAiXLbYbHrqrx1pYaYTPCIyJS0yGziwc2GlKlKQtGPFvNVEcc38mD/HVjikK0u7g0EHz1IA9EYFbsDgHD2B5uLtVsrYPZdglDNaooUA/oOsIiwFGPphu33bOlwuO09Ez/aT5npofa7zQ1EGWIEpmNCZ94tHsh15AoGAOc6B3n2crg9+2sBfo7f9qY0enreur7TtfPyiqRs0ba5PSSaPx9+f1h8VRsA1R15ABJKOSBzkyWLRniDzBoS3odUx9AhaKzip9bPz+s1I3IcVinOx1QxT0ncSwRN4dyuMeL0KayjSvNIgBrpyN6Uf5ROlXHEyYjC51GH01ihNZGk=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyQQceVUChTJGtF/a8SXufhSxDTKporieTq9NO7yDZSpDlAX1zVPT/nf0KWAlxq1TYappWMIYtyrOABhJyn6flNP6vuSBiM5lYsepHvYrtRHqlFiJruEkiaCgEZBKL5aCfBHYj0oqgQn9MpNV/PEH4cBYAVaiI4+VX8CBUQfeEGjgN6OkpLULZ3X0JUkmSnVvCNJ1m3PD68IIlbOfEZXJUKCqmZhzprGR5VWswjxA+g87cMwvijL4gdkSy/daG62Bz5vApcmmMkuX1k1fMWP4ajZCASVw8HD+MSLRhd8We9F97gd8CW0TavzbdR+mTS5H4yEgO8F9HRAsbkhV9yu0yQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://haaj5h.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.mall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
